<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 15:17
 */

namespace app\admin\controller;
use PHPExcel;
use PHPExcel_IOFactory;
use app\admin\logic\ExchangeLogic;
use think\facade\Request;
use app\common\model\Site;
use app\common\model\SiteTeam;
class Exchange extends Base
{

    private $ExchangeLogic;

    public function __construct()
    {
        parent::__construct();
        $this->ExchangeLogic = new ExchangeLogic();
    }

    public function export(){
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
// 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
// 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '序号');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '昵称');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '手机号码');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '核销码');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '是否核销');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '兑换时间');

        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->ExchangeLogic->applyList($where, $where['page']);
        $data = $res['data'];
        $num = 2;
        foreach ($data as $k => $v) {
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, ($k+1));
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['nickname']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['mobile']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['code']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['status']?'已核销':'未核销');
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, ' '.$v['create_time']);
            $num++;
        }
        $fileName = "积分兑换表" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        $objPHPExcel->getActiveSheet()->setTitle('sheet'); // 设置工作表名
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //下载 excel5与excel2007
        ob_end_clean(); // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }
    public function exports(){
        $titleName = time();
        $tableheader = array('序号','昵称','手机号码','核销码','是否核销','兑换时间');
        $html = "\xEF\xBB\xBF";
        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->ExchangeLogic->applyList($where, $where['page']);
        $dataArr = $res['data'];
        $count=floor(count($tableheader)/2);
        for ($i=0; $i < $count; $i++) {
            $html.="\t ,";
        }

        $html .=date('Y-m-d')."积分兑换表\t ,\n";

        foreach ($tableheader as $value) {
            $html .= $value . "\t ,";
        }

        $html .= "\n";
        foreach ($dataArr as $mid => $value) {
            $html .= $mid+1 . "\t ,";
            $html .= $value['nickname'] . "\t ,";
            $html .= $value['mobile'] . "\t ,";
            $html .= $value['code'] . "\t ,";
            $status = $value['status']?'已核销':'未核销';
            $html .= $status . "\t ,";
            $html .= $value['create_time'] . "\t ,";
            $html .= "\n";
        }
        $filename = $titleName;
        header("Content-type:text/csv");
        header("Content-Disposition:attachment; filename=".$filename.".csv");

        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        exit($html);
    }

    public function index(){
        return $this->fetch();
    }

    public function apply(){
        $this->assign('id',Request::param('id'));
        return $this->fetch();
    }

    public function ajaxApply(){
        $where = Request::param();
        $res = $this->ExchangeLogic->applyList($where, self::$nowPage);
        if(Request::param('export')){
            $this->export();
            die;
        }
        return json($res);
    }

    public function ajaxIndex(){
        $where = Request::param();
        $res = $this->ExchangeLogic->ExchangeList($where, self::$nowPage);
        return json($res);
    }
    public function edits(){
        $id = Request::param('id');
        $info = ExchangeLogic::get_info($id);
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function add(){
        $id = Request::get('id');
        $act = 'add';
        if ($id){
            $act = 'edit';
            $cateInfo = $this->ExchangeLogic->info($id);
            $this->assign('info', $cateInfo['data']);
        }
        $where = [];
        $site_id = $_SESSION['xm_travel']['admin_info']['site_id'];
        if($site_id){
            $where[] = ['id','=',$site_id];
        }
        $this->assign('site',Site::where($where)->select());
        $this->assign('act', $act);
        return $this->fetch();
    }

    public function handle(){
        $data = Request::param();
        $res = $this->ExchangeLogic->handle($data);
        return json($res);
    }


}